package rad.framework.mda.actions;

import java.util.Iterator;

import rad.framework.mda.util.ModelUtil;

import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.uml2.common.edit.command.ChangeCommand;
import org.eclipse.uml2.uml.Element;

/**
 * @author boudyacho
 * 
 */
public class DestroyElementAction extends Action {

	private static final String LABEL = "Delete";

	private IStructuredSelection selection;

	/**
	 * @param selection
	 */
	public DestroyElementAction(ISelection selection) {
		super();
		this.selection = (IStructuredSelection) selection;
		setText(LABEL);
	}

	/**
	 * @return
	 */
	protected Element getElement() {
		return (Element) selection.getFirstElement();
	}

	/**
	 * @return
	 */
	protected TransactionalEditingDomain getEditingDomain() {
		return TransactionUtil.getEditingDomain(getElement());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@SuppressWarnings("unchecked")
	public void run() {
		getEditingDomain().getCommandStack().execute(
			new ChangeCommand(getEditingDomain(), new Runnable() {
				public void run() {
					for (Iterator i = selection.iterator(); i.hasNext();) {
						Object e = (Object) i.next();
						if (e instanceof Element) {
							Element element = (Element) e;
							ModelUtil.clean(element, element);
							element.destroy();
						}
					}
				}
			}, LABEL));
	}
}